package v1

import (
	"github.com/gin-gonic/gin"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/router/api"
	"zimuzu_cvn_web_api/router/api/web/middleware"
	"zimuzu_cvn_web_api/service"
)

type DynamicAPI struct {
}

func (a DynamicAPI) Name() string {
	return "dynamic API"
}

func (a DynamicAPI) Bind() {
	dynamicAPI := config.C.Router.Group("/v1/dynamic", middleware.TrackLog())
	{
		dynamicAPI.GET("cvn_weekly", weekly)
		dynamicAPI.GET("cvn_news", news)
	}
}

func weekly(c *gin.Context) {
	var params = model.NewDefaultPageParams()
	err := c.ShouldBindQuery(&params)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}

	res, err := service.GetWeeklyList(params.Per, params.Page)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	errno.OK.Info = "返回cvn新闻报道成功"
	api.SendResponse(c, errno.OK, res)
}

func news(c *gin.Context) {
	var params = model.NewDefaultPageParams()
	err := c.ShouldBindQuery(&params)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}

	res, err := service.GetNewsList(params.Per, params.Page)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	errno.OK.Info = "返回cvn项目周报成功"
	api.SendResponse(c, errno.OK, res)
}
